<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * View file: form
 *
 * @author luubk
 * @since 12-06-2012 - 02:19:11
 */
?>
<div id="form_result"></div>
<article class="module width_full">
    <form method="POST" id="form" 
          action="<?php echo $id ? site_url("apps/form/$id") : site_url("apps/form") ?>">
        <header><h3><?php echo $title ?></h3></header>
        <div class="module_content">

            <fieldset style="width:48%; float:right;">
                <label>Nền tảng ứng dụng</label>
                <select name="pf_id" required="required">
                    <?php if ($platforms): ?>
                        <?php foreach ($platforms as $pl): ?>
                            <option value="<?php echo $pl['pf_id'] ?>" 
                                    <?php echo ($item['pf_id'] == $pl['pf_id']) ? 'selected=""' : '' ?>>
                                        <?php echo $pl['pf_name'] ?>
                            </option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </fieldset>
            <?php if (isset($item['app_eid'])): ?>
                <input type="hidden" name="app_eid" id="app_eid" value="<?php echo $item['app_eid']; ?>" />
            <?php endif; ?>

            <input type="hidden" name="app_vote_total" id="app_vote_total" value="<?php echo (isset($item['app_vote_total']) ? $item['app_vote_total'] : 0); ?>" />
            <input type="hidden" name="app_vote_star" id="app_vote_star" value="<?php echo (isset($item['app_vote_star']) ? $item['app_vote_star'] : 0); ?>" />

            <fieldset style="width:48%; float:left; margin-right: 3%;">
                <label>Tên ứng dụng</label>
                <input type="text" style="width:92%;" id="News_news_title"
                       onkeyup="char_limit(this, 70, '#max_title'); url_title(this, '#title_url')"
                       value="<?php echo isset($item['app_name']) ? $item['app_name'] : '' ?>"
                       name="app_name" required="required">
                (Tối đa <span id="max_title">70</span> ký tự)
                <div class="clear"></div>
                <br/>
                <input type="text" style="width:92%;" id="title_url"
                       value="<?php echo isset($item['app_name_url']) ? $item['app_name_url'] : '' ?>"
                       name="app_url" required="required" placeholder="URL cho ứng dụng">
            </fieldset>

            <fieldset style="width:48%;float:right;">
                <label>Danh mục ứng dụng</label>               
                <div class="clear"></div>
                <?php if (isset($cate_tree)): ?>                    
                    <div style="height: 270px; overflow: auto;">
                        <?php echo $cate_tree ?>               
                    </div>                    
                <?php endif; ?>
            </fieldset>

            <fieldset style="width:24%; float:left; margin-right: 3%;">
                <label>Hình đại diện</label>
                <div class="clear"></div>
                <div style="padding: 0 10px">                    
                    <span>
                        <a href="#" id="thumb_link">
                            <?php
                            $thumb = ($item['app_thumb'] != '') ? site_url('photo/gate/' . urlencode(urlencode($item['app_thumb']))) : base_url() . 'assets/css/_common/images/image.png';
                            ?>
                            <img src="<?php echo $thumb ?>" 
                                 style="max-height:128px;max-width:128px"/>
                        </a>
                        <input type="hidden" value="<?php echo $item['app_thumb'] ?>" name="app_thumb"/>
                    </span>                                            
                </div>
            </fieldset>

            <fieldset style="width:21%; float:left; margin-right: 3%;">
                <label>Giá</label>
                <input type="text" style="width:80%;" value="<?php echo isset($item['app_price']) ? $item['app_price'] : '' ?>"
                       name="app_price" required="required">
            </fieldset>

            <fieldset style="width:21%; float:left; margin-right: 3%;">
                <label>Phiên bản</label>
                <input type="text" style="width:80%;" value="<?php echo isset($item['app_version']) ? $item['app_version'] : '' ?>"
                       name="app_version" required="required">
            </fieldset>

            <fieldset style="width:48%; float:left; margin-right: 3%;">
                <label>Thứ tự</label>
                <input type="text" style="width:92%;" value="<?php echo isset($item['app_rank']) ? $item['app_rank'] : 0 ?>"
                       name="app_rank" required="required">
                =0 &LongRightArrow; <span style="font-size: 1.5em;color: goldenrod">&star;</span>; 
                >0 &LongRightArrow; <span style="font-size: 1.5em;color: goldenrod">&starf;</span>; 
                thứ tự lớn xếp trên
            </fieldset>

            <fieldset style="width:48%; float:right;">
                <label>Tóm tắt</label>
                <textarea rows="4" style="width:92%;" 
                          name="app_summary"><?php echo isset($item['app_summary']) ? $item['app_summary'] : '' ?></textarea>
                Hiển thị trên danh sách ứng dụng
            </fieldset>            

            <fieldset style="width:48%; float:left;">
                <label>SEO</label>
                <textarea rows="4" style="width:92%;" 
                          name="app_seo"><?php echo isset($item['app_seo']) ? $item['app_seo'] : '' ?></textarea>
                Nội dung hiển thị cho bộ máy tìm kiếm
            </fieldset> 

            <fieldset style="width:100%; float:left;">
                <label>Mô tả</label>
                <textarea rows="10" style="width:92%;" 
                          name="app_desc"><?php echo isset($item['app_desc']) ? $item['app_desc'] : '' ?></textarea>
                Hiển thị trên chi tiết ứng dụng
            </fieldset>          
            <div class="clear"></div>

            <fieldset>
                <label>File cài ứng dụng</label>
                <div class="clear"></div>
                <div style="padding: 0 10px">
                    <table class="tablesorter" cellspacing="0" style="margin: 0">         
                        <thead>
                            <tr>
                                <th width="20%">File cài</th>
                                <th width="10%">Kích cỡ</th>
                                <th width="20%">Dòng điện thoại hỗ trợ</th>
                                <th width="20%">Hệ điều hành hỗ trợ</th>
                                <th width="10%">Weight/Height</th>
                                <th width="10%">CLDC/MIDP</th>
                                <th width="10%">HĐ</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (isset($app_file) && !empty($app_file)): ?>
                                <?php foreach ($app_file as $file): ?>
                                    <?php $av_id = 'av_' . $file['av_id'] ?>
                                    <tr id="<?php echo $av_id ?>">
                                        <td>
                                            <b><?php echo basename($file['av_file']) ?></b>
                                            <?php if (!file_exists(UPLOAD_DIR . $file['av_file'])): ?>
                                                <p style="color:red">                                                
                                                    * File không tồn tại trên hệ thống, bạn vui lòng cập nhật lại
                                                </p>                                                
                                            <?php endif; ?>
                                            <input type="hidden" value="<?php echo $file['av_id'] ?>" name="apps_file[<?php echo $av_id ?>][av_id]">
                                            <input type="hidden" value="<?php echo $file['av_file'] ?>" name="apps_file[<?php echo $av_id ?>][av_file]">
                                        </td>                                        
                                        <td>
                                            <?php echo byte_format($file['av_size']) ?>
                                        </td>                                        
                                        <td>
                                            <textarea rows="5" name="apps_file[<?php echo $av_id ?>][av_phone]"><?php echo $file['av_phone'] ?></textarea>
                                        </td>
                                        <td>
                                            <textarea rows="5" name="apps_file[<?php echo $av_id ?>][av_os_version]"><?php echo $file['av_os_version'] ?></textarea>
                                        </td>
                                        <td>
                                            <input type="text" size="5" name="apps_file[<?php echo $av_id ?>][av_resolution_width]" 
                                                   value="<?php echo $file['av_resolution_width'] ?>" />
                                            <br/>
                                            <br/>
                                            <input type="text" size="5" name="apps_file[<?php echo $av_id ?>][av_resolution_height]" 
                                                   value="<?php echo $file['av_resolution_height'] ?>" />                                            
                                        </td>
                                        <td>
                                            <input type="text" size="5" name="apps_file[<?php echo $av_id ?>][av_cldc]" 
                                                   value="<?php echo $file['av_cldc'] ?>" />
                                            <br/>
                                            <br/>
                                            <input type="text" size="5" name="apps_file[<?php echo $av_id ?>][av_midp]" 
                                                   value="<?php echo $file['av_midp'] ?>" />
                                        </td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <a href="#" onclick="return delete_av_file('<?php echo $file['av_id'] ?>');">Xóa</a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                            <tr id="app_link">
                                <td colspan="4">
                                    <a href="#">Upload ứng dụng</a>
                                </td>
                            </tr>
                        </tbody>
                    </table>      
                </div>                
            </fieldset>
            <div class="clear"></div>

            <fieldset>
                <label>Ảnh chụp màn hình</label>
                Bấm vào một ảnh để xóa ảnh đó. Ảnh chỉ bị xóa hoàn toàn nếu bấm "Lưu lại".
                <div class="clear"></div>
                <div style="padding: 0 10px">
                    <?php if (isset($item['app_screenshot'])): ?>
                        <input type="hidden" value="<?php echo $item['app_screenshot'] ?>" name="current_screenshot"/>
                        <?php if (@$item['app_screenshot'] != ''): ?>
                            <?php foreach (explode(",", $item['app_screenshot']) as $screenshot): ?>
                                <span onclick="return del_screenshot($(this), '<?php echo $screenshot ?>')">
                                    <img src="<?php echo site_url('photo/gate/' . urlencode(urlencode($screenshot))) ?>" 
                                         style="max-height:128px;max-width:128px"/>
                                    <input type="hidden" value="<?php $screenshot ?>" name="screenshot[]"/>
                                </span>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    <?php endif; ?>
                    <a href="#" id="photo_link">
                        <img src="<?php echo base_url() . 'assets/css/_common/images/image_add.png' ?>"/>
                    </a>
                </div>
            </fieldset>
        </div>
        <footer>
            <div class="submit_link">
                <span class="loading">&nbsp;</span>
                <select name="app_status">
                    <option value="1" <?php echo (isset($item['app_status']) && $item['app_status'] == 1) ? 'selected="selected"' : '' ?>>Kích hoạt</option>
                    <option value="0" <?php echo (isset($item['app_status']) && $item['app_status'] == 0) ? 'selected="selected"' : '' ?>>Vô hiệu hóa</option>
                </select>
                <input type="submit" value="Lưu lại" class="alt_btn">
                <input type="reset" value="Nhập lại">                
            </div>
        </footer>
    </form>
</article><!-- end of post new article -->
<div id="thumb_dialog" title="Upload ảnh đại diện cho ứng dụng từ máy của bạn">
    <div id="thumb_result" style="text-align: center"></div>
    <form method="POST" enctype="multipart/form-data"
          action="<?php echo site_url('apps/upload_thumb') ?>" id="thumb_frm" >        
        <input type="file" name="userfile" size="20" id="thumb_file"/>        
        <div class="progressbar"></div>
    </form>    
</div>
<div id="photo_dialog" title="Upload ảnh từ máy của bạn">
    <div id="photo_result" style="text-align: center"></div>
    <form method="POST" enctype="multipart/form-data"
          action="<?php echo site_url('apps/upload_photo') ?>" id="photo_frm" >        
        <input type="file" name="userfile" size="20" id="photo_file"/>        
        <div class="progressbar"></div>
    </form>    
</div>
<div id="app_dialog" title="Upload file cài ứng dụng từ máy của bạn">
    <div id="app_result" style="text-align: center"></div>
    <form method="POST" enctype="multipart/form-data"
          action="<?php echo site_url('apps/upload_app') ?>" id="app_frm" >        
        <p>            
            <input type="file" name="userfile" class="app_file"/>        
        </p>
        <p>
            Up một file .zip chứa các thư mục, trong thư mục đó là file cài đặt 
            và dòng điện thoại hỗ trợ tương ứng.<br/>            
        </p>            
        <p style="text-align: center">
            <a href="<?php echo $assets ?>images/zip_mau.png" target="_blank" style="color: blue">
                Xem cấu trúc file .zip mẫu
            </a>
        </p>
        <div class="progressbar"></div>
    </form>    
</div>
<script>
    function delete_av_file(id)
    {
        av_id = '#av_' + id;
        if(confirm('Bạn có chắc là muốn xóa file này không?'))
        {
            $(av_id).remove();
            del_file_input = '<input type="hidden" name="av_del[]" value="' + id + '"/>';
            $('#app_link').after(del_file_input);
        }
        
        return false;
    }
    function del_screenshot(span, img)
    {
        if(confirm('Bạn có chắc là muốn xóa ảnh này không?'))
        {
            span.remove();
            del_file_input = '<input type="hidden" name="screenshot_del[]" value="' + img + '"/>';
            $('#photo_link').after(del_file_input);
        }
        
        return false;
    }
</script>